/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataset;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import parse.CCGDerivation;
import parse.GMBXMLParser;

/**
 *
 * @author phong
 */
public class Data {

    List<CCGDerivation> derivations;
    
    public Data() {
        this.derivations = new LinkedList<CCGDerivation>();
    }
    
    public List<CCGDerivation> getDerivations() {
        return this.derivations;
    }
    
    public void loadFromFiles(String dirPath) {
        GMBXMLParser xmlParser = new GMBXMLParser();

        try {
            String path = dirPath + "/data";
            File dir = new File(path);
            String[] subnames = dir.list();
            
            for (String subname : subnames) {            
                File subdir = new File(path + "/" + subname);
                String[] subsubnames = subdir.list();
                
                for (String subsubname : subsubnames) {
                    
                    // reading CCG derivations
                    File file = new File(path + "/" + subname + "/" + subsubname + "/en.der.xml");

                    System.out.println("--------------------------------------------------------");
                    System.out.println(subname + "/" + subsubname);
                    
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    
                    dbf.setValidating(false);
                    dbf.setFeature("http://xml.org/sax/features/namespaces", false);
                    dbf.setFeature("http://xml.org/sax/features/validation", false);
                    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
                    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document doc = db.parse(file);
                    doc.getDocumentElement().normalize();

                    NodeList childNodes = ((Element)doc.getElementsByTagName("xdrs-output").item(0)).getElementsByTagName("der"); 

                    for (int i = 1; i <= childNodes.getLength(); i++) {
                        Element elm = (Element)childNodes.item(i-1);
                        try {
                            CCGDerivation der = xmlParser.parseDerivation(elm);
                            this.derivations.add(der);
                        }
                        catch(Error e) {
                            e.printStackTrace();
                        }
                        catch(Exception e) {
                            e.printStackTrace();
                        }
                        
                    }
                }
            }
                        
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }
    
    public static void main(String[] args) {
        Data data = new Data();
        data.loadFromFiles("../gmb-small");
    }
}
